
package com.mego.tzadd.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.KeyEvent;
import android.widget.LinearLayout;

import com.mego.tzadd.R;
import com.mego.tzadd.database.dbmodel.SystemAllInfo;
import com.mego.tzadd.database.manager.SystemCenter;
import com.mego.tzadd.utils.CrashHandler;
import com.mego.tzadd.utils.Utils;

import zuo.biao.library.base.BaseBottomTabActivity;
import zuo.biao.library.interfaces.OnBottomDragListener;


public class MainTabActivity extends BaseBottomTabActivity implements OnBottomDragListener {
    private final String TAG = "MainTabActivity";

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_tab_activity, this);

        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());
        Thread.setDefaultUncaughtExceptionHandler(crashHandler);


        if (SystemCenter.getInstance().adminid == 0) {
            showShortToast("無登錄記錄");
            finish();
            return;
        }

        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        //功能归类分区方法，必须调用>>>>>>>>>>
        //apk版本升级
//        UpdateManager.getUpdateManager().checkAppUpdate(this, false);

//        Intent start = new Intent(context, MyService.class);
//        context.startService(start);

    }

    // UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    LinearLayout llBottomTab;

    @Override
    public void initView() {// 必须调用
        super.initView();
        exitAnim = R.anim.bottom_push_out;
        llBottomTab = (LinearLayout) findViewById(R.id.llBottomTab);
    }

    @Override
    protected int[] getTabClickIds() {
        return new int[]{R.id.llBottomTabTab0, R.id.llBottomTabTab1, R.id.llBottomTabTab2};
    }

    @Override
    protected int[][] getTabSelectIds() {
        return new int[][]{
                new int[]{R.id.ivBottomTabTab0, R.id.ivBottomTabTab1, R.id.ivBottomTabTab2},//顶部图标
                new int[]{R.id.tvBottomTabTab0, R.id.tvBottomTabTab1, R.id.tvBottomTabTab2}//底部文字
        };
    }

    @Override
    public int getFragmentContainerResId() {
        return R.id.flMainTabFragmentContainer;
    }

    Fragment0 _fragment0;
    Fragment1 _fragment1;
    Fragment2 _fragment2;


    @Override
    protected Fragment getFragment(int position) {
        if (_fragment0 == null) {
            _fragment0 = Fragment0.createInstance();
        }
        if (_fragment1 == null) {
            _fragment1 = Fragment1.createInstance();
        }
        if (_fragment2 == null) {
            _fragment2 = Fragment2.createInstance();
        }

        switch (position) {
            case 1:
                return _fragment1;
            case 2: {
                return _fragment2;
            }
            default: {
                return _fragment0;
            }
        }
    }

    @Override
    protected void selectTab(int position) {
        //导致切换时闪屏，建议去掉BottomTabActivity中的topbar，在fragment中显示topbar
        if (Utils.isFastDoubleClick())
            return;

        String[] TAB_NAMES = getResources().getStringArray(R.array.tab_names);
        tvBaseTitle.setText(TAB_NAMES[position]);

    }
    // UI显示区(操作UI，但不存在数据获取或处理代码，也不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    // Data数据区(存在数据获取或处理代码，但不存在事件监听代码)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initData() {// 必须调用
        super.initData();
    }
    // Data数据区(存在数据获取或处理代码，但不存在事件监听代码)>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    // Event事件区(只要存在事件监听代码就是)<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    @Override
    public void initEvent() {// 必须调用
        super.initEvent();

    }

    @Override
    public void onDragBottom(boolean rightToLeft) {
        //将Activity的onDragBottom事件传递到Fragment，非必要<<<<<<<<<<<<<<<<<<<<<<<<<<<
        switch (currentPosition) {
            case 2:
                break;
            default:
                break;
        }
        //将Activity的onDragBottom事件传递到Fragment，非必要>>>>>>>>>>>>>>>>>>>>>>>>>>>
    }


    private long firstTime = 0;//第一次返回按钮计时

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {

        switch (keyCode) {
            default:
                break;
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {
                    showShortToast(getString(R.string.texit));
                    firstTime = secondTime;
                } else {//完全退出
                    moveTaskToBack(false);//应用退到后台
                    System.exit(0);
                }
                break;
        }

        return true;
    }


}